<template>
  <div class="dialog">
    <el-dialog :title="'物料' + titleText" :visible.sync="showDialog" width="40%" :close-on-click-modal="false"
      @close="cancel">

      <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="120px" class="demo-ruleForm">
        <el-form-item label="物料名称" prop="name">
          <el-input v-model="formData.name" clearable placeholder="请输入物料名称" />
        </el-form-item>
        <el-form-item label="物料类型" prop="materialTypeNo">
          <el-select class="w100p" filterable clearable v-model="formData.materialTypeNo">
            <el-option v-for="(item, index) in materialType" :key="index" :label="item.materialTypeName"
              :value="item.materialTypeNo" />
          </el-select>
        </el-form-item>
        <el-form-item label="规格数量" prop="ratio" class="w100p">
          <el-input class="w100p" v-model="formData.ratio" clearable placeholder="请输入单位数量" />
        </el-form-item>
        <el-form-item label="主单位" prop="basicUnitId" class="w100p">
          <el-select class="w100p" filterable clearable v-model="formData.basicUnitId" placeholder="主单位">
            <el-option v-for="item, index in unit" :key="index" :label="item.unitName" :value="item.unitId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="辅助单位" prop="auxiliaryUnitId">
          <el-select class="w100p" filterable clearable v-model="formData.auxiliaryUnitId" collapse-tags
            placeholder="辅助单位">
            <el-option v-for="item, index in unit" :key="index" :disabled="formData.basicUnitId == item.unitId"
              :label="item.unitName" :value="item.unitId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="物料品牌" prop="brand">
          <el-input v-model="formData.brand" clearable placeholder="请输入物料品牌" />
        </el-form-item>
        <el-form-item label="物料型号" prop="materialType">
          <el-input v-model="formData.materialType" clearable placeholder="请输入物料型号" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="formData.remark" clearable :rows="2" type="textarea" placeholder="请输入描述" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import MaterialApi from "@/api/basic-info-manage/material"
import MaterialTypeManageApi from "@/api/basic-info-manage/material-type-manage"
import Material from "@/api/basic-info-manage/unit-manage"  //物料单位
// import EnumApi from "@/api/enum"
export default {
  name: '',
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      alisss: '',
      unit: [],
      formData: {},
      rules: {
        name: [
          { required: true, message: "请输入物料名称", trigger: ["change", "blur"] }
        ],
        materialTypeNo: [
          { required: true, message: "请选择物料类型", trigger: ["change", "blur"] }
        ],
        ratio: [
          { required: true, message: "请输入数量", trigger: ["change", "blur"] }
        ],
        basicUnitId: [
          { required: true, message: "请选择主单位", trigger: ["change", "blur"] }
        ],
        auxiliaryUnitId: [
          { required: true, message: "请选择辅助单位", trigger: ["change", "blur"] }
        ],
      },
      materialType: [
        {
          typeName: "饲料",
          typeCode: "FEED",
        },
        {
          typeName: "药品",
          typeCode: "DRUGS",
        },
        {
          typeName: "物料",
          typeCode: "MATERIAL",
        },
      ],
      showDialog: this.show
    };
  },
  computed: {
  },
  watch: {
    show(v) {
      this.showDialog = v;
    },
    nowChanceStau(v) {
      if (v === 'add') {
        this.formData = {
          staffName: sessionStorage.getItem('staffName'),
        }
      }
      if (v === 'edit') {
        this.formData = this.editObject
      }
      this.$nextTick(() => {
        this.$refs['ruleForm'] && this.$refs['ruleForm'].clearValidate()
      })
    },
  },
  created() {
  },
  mounted() {
    this.queryMaterialType()
    this.queryPageByCondition()
  },
  methods: {
    // 物料类型
    queryMaterialType() {
      MaterialTypeManageApi.queryByCondition().then(res => {
        this.materialType = res.data
      })
    },
    // queryEnum(){
    //   EnumApi.list({enumType:'MaterialType'}).then(res=>{
    //     this.materialType = res.data
    //   })
    // },

    async queryMaterial() {
      const res = await MaterialApi.queryByCondition({})
      this.MaterialList = res.data
    },

    // 物料单位条件查询
    queryPageByCondition() {
      Material.queryByCondition({}).then(res => {
        this.unit = res.data
      })
    },
    getUnitName(id) {
      let rut = this.unit.find(item => {
        return item.unitId == id
      })
      return rut.unitName
    },

    save(formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          this.formData = {
            ...this.formData,
            auxiliaryUnitName: this.getUnitName(this.formData.auxiliaryUnitId),
            basicUnitName: this.getUnitName(this.formData.basicUnitId)
          }
          if (this.nowChanceStau === "edit") {
            // if(!this.formData.auxiliaryUnitId){
            //   this.formData.auxiliaryUnitName=''
            // }
            console.log(this.formData)
            // this.unit
            MaterialApi.edit(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("修改成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          } else if (this.nowChanceStau === "add") {
            MaterialApi.add(this.formData).then(res => {
              if (res.success === true) {
                console.log(3333333);

                this.$message.success("添加成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    close() {
      this.$emit("closeDialog");
    },
    cancel() {
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped></style>
